<?php

namespace app\admin\controller;
use app\admin\controller\Main;
use think\Controller;
use think\Db;

class Cash extends Main {

    public function index() {

        $_GPC = input();
        $page = max(intval($_GPC['page']), 1);
        $limit = max(intval($_GPC['limit']), 10);
        $condition = 'c.`uniacid` = :uniacid';
        $db = Db::name('cash_log')
                        ->alias('c')
                        ->join("member m", 'c.member_id=m.id')->field('c.*,m.nickname,m.avatar,m.mobile');

        $count = $db->count();
        $db->order('c.status,c.createtime DESC')->page($page, $limit);

        $data = $db->select();
        jsonp($data, $count);
    }

    public function tg() {

        $_GPC = input();
        $cash_log = Db::name('cash_log')->where(array('id' => $_GPC['id']))->get();
        $member = Db::name('member')->where(array('id' => $cash_log['member_id']))->get();
        if ($cash_log['status'] == 0) {
            if ($cash_log['type'] == 0) {
                pdo_update('member', array('credit1' => $member['credit1'] - $cash_log['price'], 'num' => $member['num'] == 0 ? 0 : $member['num'] - $cash_log['num'], 'nums' => $member['nums'] - $cash_log['num']), array('id' => $member['id']));
            } else {
                pdo_update('member', array('credit2' => $member['credit2'] + $cash_log['price']), array('id' => $member['id']));
            }
            $result = pdo_update('cash_log', array('status' => 1, 'updatetime' => time()), array('id' => $_GPC['id']));
            if (!empty($result) || $result === 0) {
                json('success');
            } else {
                json('error', 1, '操作失败！');
            }
        }
    }

    public function jj() {

        $_GPC = input();
        $result = pdo_update('cash_log', array('status' => 2, 'updatetime' => time()), array('id' => $_GPC['id']));
        if (!empty($result) || $result === 0) {
            json('success');
        } else {
            json('error', 1, '操作失败！');
        }
    }

}
